package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qaml.mes.device.dao.CheckUpkeepMessageDO;
import com.qaml.mes.device.dao.mapper.CheckUpkeepMessageMapper;
import com.qaml.mes.device.repository.CheckUpkeepMessageRepository;
import com.qaml.mes.device.dto.eqp.CheckUpkeepMessageDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CheckUpkeepMessageRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/3/4 0004 11:27
 * @Version 1.0
 */
@Repository
public class CheckUpkeepMessageRepositoryImpl implements CheckUpkeepMessageRepository {


    @Resource
    private CheckUpkeepMessageMapper checkUpkeepMessageMapper;

    @Override
    public void saveBatch(List<CheckUpkeepMessageDO> checkUpkeepMessages) {
        checkUpkeepMessageMapper.saveBatch(checkUpkeepMessages);
    }

    @Override
    public IPage<CheckUpkeepMessageDO> pageList(CheckUpkeepMessageDTO checkUpkeepMessageDTO) {
        LambdaQueryWrapper<CheckUpkeepMessageDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepMessageDO::getIsDeleted, 0);
        queryWrapper.orderByDesc(CheckUpkeepMessageDO::getSendTime);
        queryWrapper.eq(CheckUpkeepMessageDO::getIsRead, 0);
        queryWrapper.eq(CheckUpkeepMessageDO::getUserId, checkUpkeepMessageDTO.getUserId());
        queryWrapper.orderByDesc(CheckUpkeepMessageDO::getId);
        return checkUpkeepMessageMapper.selectPage(new Page<>(checkUpkeepMessageDTO.getPage(), checkUpkeepMessageDTO.getPageSize()), queryWrapper);

    }

    @Override
    public CheckUpkeepMessageDO getById(String id) {
        LambdaQueryWrapper<CheckUpkeepMessageDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepMessageDO::getId, id);
        queryWrapper.eq(CheckUpkeepMessageDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepMessageDO::getIsRead, 0);
        return checkUpkeepMessageMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateById(CheckUpkeepMessageDO messageDO) {
        checkUpkeepMessageMapper.updateById(messageDO);
    }

    @Override
    public List<CheckUpkeepMessageDO> getUnReadMessageList(String currentUserId) {
        LambdaQueryWrapper<CheckUpkeepMessageDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepMessageDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepMessageDO::getIsRead, 0);
        queryWrapper.eq(StringUtils.isNotBlank(currentUserId), CheckUpkeepMessageDO::getUserId, currentUserId);
        return checkUpkeepMessageMapper.selectList(queryWrapper);
    }

    @Override
    public void updateByIds(List<String> ids, String userId, String userName) {
        checkUpkeepMessageMapper.updateByIds(ids, userId, userName);
    }

    @Override
    public List<CheckUpkeepMessageDO> getNoDeletedMessage(Date startDateByMonth, Date endDateByMonth) {
        LambdaQueryWrapper<CheckUpkeepMessageDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepMessageDO::getIsDeleted, 0);
        queryWrapper.ge(CheckUpkeepMessageDO::getCheckUpkeepDate, startDateByMonth);
        queryWrapper.le(CheckUpkeepMessageDO::getCheckUpkeepDate, endDateByMonth);
        return checkUpkeepMessageMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteAll(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        checkUpkeepMessageMapper.deleteAll(ids);
    }

    @Override
    public void deleteByRecordId(String recordId, String type) {
        checkUpkeepMessageMapper.deleteByRecordIdAndType(recordId, type);
    }
}